package com.project.demo.service.base;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AliPayService {

    @Value("${alipay-gateway}")
    private String gateway;

    @Value("${alipay-appid}")
    private String appId;

    @Value("${alipay-private-key}")
    private String privateKey;

    @Value("${alipay-public-key}")
    private String publicKey;

    public String getQrCode(String goodsName,double payMoney){
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(gateway,appId,privateKey,"json","utf-8",publicKey,"RSA2");
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
            request.setBizModel(model);
            model.setOutTradeNo(String.valueOf(System.currentTimeMillis()));
            model.setSubject(goodsName);
            model.setTotalAmount(String.valueOf(payMoney));
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            return response.getQrCode();
        }catch (AlipayApiException e){
            e.printStackTrace();
        }
        return null;
    }



}
